package cn.china.myspringboot.realm;


import cn.china.myspringboot.entity.Permission;
import cn.china.myspringboot.entity.Role;
import cn.china.myspringboot.entity.User;
import cn.china.myspringboot.serviceimpl.PermissionServiceImpl;
import cn.china.myspringboot.serviceimpl.RoleServiceImpl;
import cn.china.myspringboot.serviceimpl.UserServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

public class CustomRealm  extends AuthorizingRealm {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    PermissionServiceImpl permissionService;


    @Override
//   1、subject.hasRole(“admin”) 或 subject.isPermitted(“admin”)等方法被调用的时候doGetAuthorizationInfo()方法会被自动调用；
//   2、@RequiresRoles("admin") 在方法上加注解的时候会调用该方法
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//获取登录用户名
        String name = (String) principalCollection.getPrimaryPrincipal();
        System.out.println("从subject的login传来的username11:"+name);
        //根据用户名去数据库查询用户信息
        User user =userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername,name));

        System.out.println("查询的user"+user);
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        //listRole==>找到jyz有admin,user两个角色
        List<Role> listRole=roleService.list(new LambdaQueryWrapper<Role>().eq(Role::getUsername,name));
        System.out.println(listRole);
        for (Role role : listRole) {

            //添加角色
            simpleAuthorizationInfo.addRole(role.getRolename());//user
            //添加权限
            //通过角色名查询角色对应的权限并添加
            List<Permission> listPermission=permissionService.list(new LambdaQueryWrapper<Permission>().eq(Permission::getRolename,role.getRolename()));
            //admin角色对应query,add两个权限
            for (Permission permission : listPermission) {
                simpleAuthorizationInfo.addStringPermission(permission.getPermissionsname());
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    //controller中subject.login()会自动调用该方法,并把username传过来,通过用户名查询数据库密码进行比较会调用该方法
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        System.out.println("当前执行doGetAuthenticationInfo");
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (authenticationToken.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String name = authenticationToken.getPrincipal().toString();
        System.out.println("从subject的login传来的username:"+name);

        //连接数据库 根据用户名查询

        User user =userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername,name));
        if (user == null) {
            //这里返回后会报出对应异常
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, user.getPassword().toString(), getName());
            System.out.println("simpleAuthenticationInfo=>"+simpleAuthenticationInfo);
            return simpleAuthenticationInfo;//能正常返回,用户名密码验证成功
        }
    }
}